package b.h.y.bj;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

import b.h.y.bj.util.SharedPreferencesUtil;

public class SplashActivity extends AppCompatActivity {

    private RelativeLayout rlRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        rlRoot = (RelativeLayout) findViewById(R.id.rl_root);

        startAnim();
    }


    /**
     * 开启动画
     */
    private void startAnim() {

        AnimationSet animationSet = new AnimationSet(false);

        //旋转动画
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(2000); //动画时间
        rotateAnimation.setFillAfter(true); //保持动画状态
        //rlRoot.startAnimation(rotateAnimation);

        //缩放动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(2000); //动画时间
        scaleAnimation.setFillAfter(true); //保持动画状态

        //渐变动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(2000); //动画时间
        alphaAnimation.setFillAfter(true); //保持动画状态

        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(alphaAnimation);

        /**
         * 设置动画监听
         */
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            //动画结束
            @Override
            public void onAnimationEnd(Animation animation) {
                goNext();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        rlRoot.startAnimation(animationSet);
    }

    private void goNext() {
        boolean isShow = SharedPreferencesUtil.getBoolean(SplashActivity.this, "is_show_guide", false);
        Intent intent;
        if (isShow) { //展示过了,就直接跳转到MainActivity
            intent = new Intent(SplashActivity.this, MainActivity.class);
        } else {
            //跳转到新手引导页
            intent = new Intent(SplashActivity.this, GuideActivity.class);
        }
        startActivity(intent);
        finish();
    }
}
